<?php 

class suipaiController extends appController
{
	
	public function clear()
	{
		unset($_SESSION['wxid']);
	}
	
	public function index()
	{
		
		
// 		$_SESSION['wxid'] = 1;
		$this->check_member();
		
		$config[c] = array('site_name' => "报料-".Model('app')->get_cfg('site_name'));
		$this->assign_global($config);
		
		$jsdk = Model('suipai')->get_jsdk();
		
		$this->assign("jsdk", $jsdk);
		
		$this->display("index.html");
	}
	
	
	public function suipai()
	{
	
	
	   
	    $this->check_member();
	
	    $config[c] = array('site_name' => "报料-".Model('app')->get_cfg('site_name'));
	    $this->assign_global($config);
	
	    $jsdk = Model('suipai')->get_jsdk();
	
	    $this->assign("jsdk", $jsdk);
	
	    $this->display("suipai.html");
	}
	
	public function send_set()
	{
	    $res = array('error' => 1, 'content' => '');
	    $info['wxid'] = $_SESSION['wxid'];
	    $is_exist = Model('suipai')->check_is_exist($info['wxid']);
	    if($is_exist > 0)
	    {
	        $res['content'] = "一天只能报料一次,你今天已经报料了。";
	        $this->showMsg($res);
	    }
	    $info['content'] = yf_empty($_POST['content'], "请输入内容。");
	    
	    $info['name'] = yf_empty($_POST['name'], "请输入姓名。");
	    
	    $info['phone'] = yf_empty($_POST['phone'], "请输入手机号。");
	    if(hy_check($info['phone'], 'm') == '#@_error' && hy_check($info['phone'], 't') == '#@_error')
	    {
	        $res['content'] = "你输入的电话格式不正确。";
	        $this->showMsg($res);
	    }
	    $img = yf_empty($_POST['img'], "请上传至少一张图片。");
	    
	    $arr = array();
	    
	    if(!is_array($img))
	    {
	        $arr[] = $img;
	    }
	    else
	    {
	        $arr = $img;
	    }
	    $info['pic'] = json_encode($arr);
	    $info['created'] = yf_time();
	    $id = Model('suipai')->insert_info($info);
	    if($id)
	    {
	        $res = array('error' => 0, 'content' => '报料成功');
	    }
	    else
	    {
	        $res['content'] = "系统繁忙";
	    }
	    $this->showMsg($res);
	}
	
	
	public function uploadData()
	{
		$res = array('error' => 1, 'content' => '');
		$info['wxid'] = $_SESSION['wxid'];
		$is_exist = Model('suipai')->check_is_exist($info['wxid']);
		if($is_exist > 0)
		{
			$res['content'] = "一天只能报料一次,你今天已经报料了。";
			$this->showMsg($res);
		}
		$info['name'] = yf_empty($_POST['name'], "请输入姓名。");
		$info['content'] = yf_empty($_POST['content'], "请输入内容。");
		$info['phone'] = yf_empty($_POST['phone'], "请输入手机号。");
		if(hy_check($info['phone'], 'm') == '#@_error' && hy_check($info['phone'], 't') == '#@_error')
		{
			$res['content'] = "你输入的电话格式不正确。";
			$this->showMsg($res);
		}
		
		
		$medias = yf_empty($_POST['media'], "请上传至少一张图片。");
		
		$img = $this->getPic($medias);
		
		$arr = array();
		
		if(!is_array($img))
		{
			$arr[] = $img;
		}
		else
		{
			$arr = $img;
		}
		
		$info['pic'] = json_encode($arr);
		
		$info['created'] = yf_time();
		$id = Model('suipai')->insert_info($info);
		if($id)
		{
			$res = array('error' => 0, 'content' => '报料成功');
		}
		else 
		{
			$res['content'] = "系统繁忙";
		}
		$this->showMsg($res);
	}
	
	
	
	
	public function getPic($medias)
	{
	     
	    $token = Model('suipai')->getAccessToken();
	     
	    $pictures = array();
	    
	    

// 	    $open=fopen("log.txt","a" );
	    
// 	    fwrite($open,"----".$medias."----");
	    
// 	    fclose($open);
	    
	    if(!is_array($medias))
	    {
	        $temp = $medias;
	        $medias = array();
	        $medias[] = $temp;
	    }
	    
	    foreach ($medias as $k => $v)
	    {
	        
	        $media_id = $v;
	         
	        $url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token."&media_id=".$media_id;
	        	
	        $img = $k.rand(0, 100).time().".jpg";
	        	
	        $img = $this->GrabImage($url, $img);
	        	
	        $temp = str_replace(ROOT_PATH, "/", $img);
	        	
            
// 	        $open=fopen("log.txt","a" );
	         
// 	        	    fwrite($open,"----".$url."----");
	         
// 	        	    fclose($open);
	        
// 	        $dst_temp = str_replace("img", "thumb", $temp);
	        	
// 	        $file = ROOT_PATH.'Uploads/thumb/'.date('Ym');
	        	
// 	        if(!is_dir($file))
// 	        {
	
	             
// 	    	       mkdir($file);
// 	        }
	        	
	        	
// 	        mkThumbnail($temp,640,null,$dst_temp);
	        	
// 	        $pic = str_replace(ROOT_PATH, "/", $img);
	        	
// 	        $thumb = str_replace("img", "thumb", $pic);
	        	
	        $pictures[] = $temp;
	        	
// 	        $pictures['thumb'][] = $thumb;
	        	
	    } 
	    return $pictures;
	
	    	
	}
	
	
	
	public function upload_img()
	{
		$res = array('error' => 1, 'content' => '');
		$media_id = yf_empty($_REQUEST['img'], "error");
		$token = Model('suipai')->getAccessToken();
		
		$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token."&media_id=".$media_id;
		$open=fopen("log.txt","a" );
		fwrite($open,$url."\r\n");
		fclose($open);
		
		$img = yf_time().".jpg";
		$img = $this->GrabImage($url, $img);
		$temp = str_replace(ROOT_PATH, "/", $img);
		$html = "<input type=\"hidden\" name=\"img\" value=\"$temp\">";
		$url = "<img src=\"$temp\" style=\"width:32%;padding:0.5%\"  class=\"pre-img\"    >";
		$res = array('error' => 0, 'content' => $html , 'url' =>$url ,'img'=>'http://g4.airefp.com/ws/'.$temp);
		$this->showMsg($res);
	}
	
	public function GrabImage($url, $filename = "") {
		if($url == ""):return false;endif;
		if($filename == "") {
			$ext = strrchr($url, ".");
			if($ext != ".gif" && $ext != ".jpg"):return false;endif;
			$filename = date("YmdHis").$ext;
		}
		ob_start();
		readfile($url);
		$img = ob_get_contents();
		ob_end_clean();
		$size = strlen($img);
// 		$file = ROOT_PATH.'Uploads/img/'.date('Ym');
		$file = 'Uploads/img/'.date('Ym');
		if(!is_dir($file))
		{
			mkdir($file);
		}
		$file = $file."/".$filename;
		$fp2 = @fopen($file, "a");
		fwrite($fp2, $img);
		fclose($fp2);
		return $file;
	}
}

?>